package com.zhanghe.springsecurity.configuration;

import com.zhanghe.springsecurity.Service.UserService;
import com.zhanghe.springsecurity.filter.AuthenticationFilter;
import com.zhanghe.springsecurity.filter.RefreshLoginFilter;
import com.zhanghe.springsecurity.handler.JWTAccessDeniedHandler;
import com.zhanghe.springsecurity.provider.CustomAuthenticationProvider;
import com.zhanghe.springsecurity.filter.LoginFilter;
import com.zhanghe.springsecurity.provider.RefreshCustomAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * SecurityConfigureation
 *
 * @author Clevo
 * @date 2018/3/25
 */
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(getRefreshCustomAuthenticationProvider());
        auth.authenticationProvider(getCustomAuthenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                    .antMatchers(HttpMethod.POST, "/login").permitAll()
                    .antMatchers("/test/*").permitAll()
                    .antMatchers("/admin/*").hasRole("ADMIN")
                    .antMatchers("/seller/*").hasRole("SELLER")
        .and().addFilterBefore(getMyLoginFilter(), UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(getRefreshLoginFilter(), UsernamePasswordAuthenticationFilter.class)
                // 添加一个过滤器验证其他请求的Token是否合法
                .addFilterBefore(getAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling().accessDeniedHandler(new JWTAccessDeniedHandler())
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    @Bean
    CustomAuthenticationProvider getCustomAuthenticationProvider(){
        return new CustomAuthenticationProvider();
    }
    @Bean
    RefreshCustomAuthenticationProvider getRefreshCustomAuthenticationProvider(){
        return new RefreshCustomAuthenticationProvider();
    }
    @Bean
    AuthenticationFilter getAuthenticationFilter(){
        return new AuthenticationFilter();
    }

    @Bean
    LoginFilter getMyLoginFilter() throws Exception{
        return new LoginFilter("/login",authenticationManager());
    }

    @Bean
    RefreshLoginFilter getRefreshLoginFilter() throws Exception{
        return new RefreshLoginFilter("/refresh",authenticationManager());
    }

}
